<?php
namespace app\xapply\model;

use think\Model;
use app\user\model\Admin;
use app\xapply\model\Login;

class Rule extends Model
{
    protected $name = 'xapply_rule';

    /**
     * $type, 类型:0=使用,1=通知,2=管理
     */
    public static function getPerm($user, $app, $type = 0, $get = false){
        $list = $app['rules'] ? $app['rules'] : [];
        $count = count($list);
        if ($get) {
            // 返回 Tokens
            if ($count > 0) {
                $uid = []; // 指定会员
                $dept = []; // 指定部门
                $group = []; // 指定会员分组
                foreach ($list as $item) {
                    if ($item['type'] == $type){
                        // a. 指定用户
                        if ($item['user_id'] > 0){
                            $uid[] = $item['user_id'];
                        }
                        // b. 指定部门
                        if ($item['dept_id'] > 0){
                            $dept[] = $item['dept_id'];
                        }
                        // c. 指定职位
                        if ($item['group_id'] > 0){
                            $group[] = $item['group_id'];
                        }
                    }
                }
                $new = [];
                if ($uid){
                    foreach ($uid as $value){
                        $new[] = $value;
                    }
                }
                if ($dept){
                    $arr = Admin::where('did', 'in', $dept)->column('id');
                    if ($arr){
                        foreach ($arr as $value){
                            $new[] = $value;
                        }
                    }
                }
                if ($group){
                    $arr = Admin::where('position_id', 'in', $group)->column('id');
                    if ($arr){
                        foreach ($arr as $value){
                            $new[] = $value;
                        }
                    }
                }
                if ($new){
                    $new = array_unique($new);
                    return Login::where('user_id', 'in', $new)->column('push', 'user_id');
                }
            }
            return [];
        }else{
            if ($count > 0) {
                foreach ($list as $item) {
                    if ($item['type'] == $type){
                        // a. 指定用户
                        if ($item['user_id'] > 0 && $item['user_id'] == $user['id']){
                            return true;
                        }
                        // b. 指定部门
                        if ($item['dept_id'] > 0 && $item['dept_id'] == $user['did']){
                            return true;
                        }
                        // c. 指定职位
                        if ($item['group_id'] > 0 && $item['group_id'] == $user['position_id']){
                            return true;
                        }
                    }
                }
            }
            if ($type == 0 && $app['is_use'] == 0){
                return true;
            }
            if ($type == 1 && $app['is_notice'] == 0){
                return true;
            }
            return false;
        }
    }
}
